package com.connection.second.wallpaper.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.connection.second.wallpaper.domain.entity.PaperAlbumWall;
import com.connection.second.wallpaper.domain.entity.PaperClassify;
import com.connection.second.wallpaper.domain.entity.PaperWall;
import com.connection.second.wallpaper.domain.vo.PaperAlbumVo;
import com.connection.second.wallpaper.domain.vo.PaperWallVo;
import com.connection.second.wallpaper.mapper.PaperAlbumWallMapper;
import com.connection.second.wallpaper.mapper.PaperClassifyMapper;
import com.connection.second.wallpaper.mapper.PaperWallMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.connection.second.wallpaper.mapper.PaperAlbumMapper;
import com.connection.second.wallpaper.domain.entity.PaperAlbum;
import com.connection.second.wallpaper.service.IPaperAlbumService;

/**
 * 专题管理Service业务层处理
 * 
 * @author FL
 * @date 2024-11-12
 */
@Service
public class PaperAlbumServiceImpl implements IPaperAlbumService 
{
    @Autowired
    private PaperAlbumMapper paperAlbumMapper;
    @Autowired
    private PaperAlbumWallMapper paperAlbumWallMapper;
    @Autowired
    private PaperWallMapper paperWallMapper;
    @Autowired
    private PaperClassifyMapper paperClassifyMapper;


    /**
     * 查询专题管理
     *
     * @param id 专题管理主键
     * @return 专题管理
     */
    @Override
    public PaperAlbumVo selectPaperAlbumById(Long id)
    {
        PaperAlbum album = paperAlbumMapper.selectPaperAlbumById(id);
        if(Objects.isNull(album)){
            return null;
        }
        PaperAlbumVo vo = new PaperAlbumVo();

       //2024.12.6为专辑榜单新增
//        复制
        BeanUtils.copyProperties(album, vo);
//        根据分类id查询分类名称
        vo.setClassName(paperClassifyMapper.selectPaperClassifyById(album.getClassId()).getName());



        //            根据专辑id查询专辑对应的壁纸
        List<PaperWallVo> pictureList = new ArrayList<>();
//            获取所有关联的壁纸
        List<PaperAlbumWall> albumWallList = paperAlbumWallMapper.selectPaperAlbumWallByAlbumId(album.getId());
        if (albumWallList!= null) {
            for (PaperAlbumWall albumWall : albumWallList) {
                PaperWall wall = paperWallMapper.selectPaperWallById(albumWall.getWallId());
                PaperWallVo wallVo = new PaperWallVo();
                BeanUtils.copyProperties(wall, wallVo);
                wallVo.setClassName(paperClassifyMapper.selectPaperClassifyById(wall.getClassId()).getName());
                pictureList.add(wallVo);
            }
            vo.setPictureList(pictureList);
        }
        return vo;
    }

    /**
     * 查询专题管理列表
     *
     * @param paperAlbum 专题管理
     * @return 专题管理
     */
    @Override
    @Cacheable(value = "paperAlbumList", key = "'selectPaperAlbumList'+#paperAlbum.toString()")
    public List<PaperAlbumVo> selectPaperAlbumList(PaperAlbum paperAlbum)
    {
        paperAlbum.setStatus(1);
        List<PaperAlbumVo> list = new ArrayList<>();
        List<PaperAlbum> albums = paperAlbumMapper.selectPaperAlbumList(paperAlbum);
        for (PaperAlbum album : albums) {
            PaperAlbumVo vo = new PaperAlbumVo();
            BeanUtils.copyProperties(album, vo);
            PaperClassify classify = paperClassifyMapper.selectPaperClassifyById(album.getClassId());
            vo.setClassName(classify.getName());
            list.add(vo);
        }
        return list;
    }

    /**
     * 新增专题管理
     * 
     * @param paperAlbum 专题管理
     * @return 结果
     */
    @Override
    public int insertPaperAlbum(PaperAlbum paperAlbum)
    {
        return paperAlbumMapper.insertPaperAlbum(paperAlbum);
    }

    /**
     * 修改专题管理
     * 
     * @param paperAlbum 专题管理
     * @return 结果
     */
    @Override
    public int updatePaperAlbum(PaperAlbum paperAlbum)
    {
        return paperAlbumMapper.updatePaperAlbum(paperAlbum);
    }

    /**
     * 批量删除专题管理
     * 
     * @param ids 需要删除的专题管理主键
     * @return 结果
     */
    @Override
    public int deletePaperAlbumByIds(Long[] ids)
    {
        return paperAlbumMapper.deletePaperAlbumByIds(ids);
    }

    /**
     * 删除专题管理信息
     * 
     * @param id 专题管理主键
     * @return 结果
     */
    @Override
    public int deletePaperAlbumById(Long id)
    {
        return paperAlbumMapper.deletePaperAlbumById(id);
    }
}
